package java2;

import java.util.Scanner;

public class Q3 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n=in.nextInt();
		int i;
		String[] id = new String[n];
		String str1, str2;
		for (i = 0; i < n; i++) {
			id[i]=in.next();
		}
		for (i = 0; i < id.length-1; i++) {
			for (int j = 0; j < id.length-1-i; j++) {
				str1 = id[j].substring(6, 14);
				str2 = id[j+1].substring(6, 14);
				if(Integer.parseInt(str1) > Integer.parseInt(str2)) {
					str1 = id[j];
					id[j] = id[j+1];
					id[j+1] = str1;
				}
			}
		}
		while (in.hasNext()) {
			str1 = in.next();
			if (str1.equals("sort1")) {
				for (i=0; i < id.length; i++) {
					String year=id[i].substring(6, 10);
					String month=id[i].substring(10, 12);
					String day=id[i].substring(12, 14);
					System.out.println(year+"-"+month+"-"+day);
				}
			}
			if (str1.equals("sort2")){
				for (i = 0; i < id.length; i++)
					System.out.println(id[i]);
			}
			if (!str1.equals("sort1") && !str1.equals("sort2"))
				System.out.println("exit");
		}
		in.close();
	}
}
